package com.front.managementfront.driver.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.front.managementfront.driver.entity.CarInfo;
import com.front.managementfront.driver.entity.CertificateImg;
import com.front.managementfront.driver.entity.HktsDriver;
import com.front.managementfront.driver.entity.HktsRealNameAuthentication;
import com.front.managementfront.driver.mapper.CarInfoMapper;
import com.front.managementfront.driver.mapper.CertificateImgMapper;
import com.front.managementfront.driver.mapper.HktsRealNameAuthenticationMapper;
import com.front.managementfront.driver.service.ICarInfoService;
import com.front.managementfront.driver.service.ICertificateImgService;
import com.front.managementfront.driver.service.IHktsDriverService;
import com.hkts.management.common.utils.SnowFlakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 证件照 服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-11-19
 */
@Service
@Transactional
public class CertificateImgServiceImpl extends ServiceImpl<CertificateImgMapper, CertificateImg> implements ICertificateImgService {
    @Autowired
    public ICarInfoService iCarInfoService;

    @Autowired
    private CertificateImgMapper certificateImgMapper;
    @Autowired
    private HktsRealNameAuthenticationMapper hktsRealNameAuthenticationMapper;
    @Autowired
    private CarInfoMapper carInfoMapper;
    @Autowired
    public IHktsDriverService iHktsDriverService;

    @Override
    public IPage<CertificateImg> selectPage(Page<CertificateImg> page, CertificateImg certificateImg) {
        QueryWrapper<CertificateImg> query = Wrappers.query();
        QueryWrapper<CertificateImg> entityWrapper = getEntityWrapper(query, certificateImg);
        return certificateImgMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<CertificateImg> selectList(CertificateImg certificateImg) {
        QueryWrapper<CertificateImg> query = Wrappers.query();
        QueryWrapper<CertificateImg> entityWrapper = getEntityWrapper(query, certificateImg);
        return certificateImgMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<CertificateImg> getEntityWrapper(QueryWrapper<CertificateImg> entityWrapper, CertificateImg certificateImg) {
        //条件拼接

        return entityWrapper;
    }

    @Override
    public void dadaHandler() {
        QueryWrapper<HktsRealNameAuthentication> query = Wrappers.query();
        List<HktsRealNameAuthentication> hktsRealNameAuthentications = hktsRealNameAuthenticationMapper.selectList(query);
        SnowFlakeUtil flowIdInstance = SnowFlakeUtil.getFlowIdInstance();
        List<CertificateImg> list = new ArrayList();
        List<CarInfo> listCarInfo = new ArrayList();
        for (int i = 0; i < hktsRealNameAuthentications.size(); i++) {
            QueryWrapper<CertificateImg> queryCertificateImg = Wrappers.query();
            queryCertificateImg.lambda().eq(CertificateImg::getUserId, hktsRealNameAuthentications.get(i).getUserId());
            Integer integer = certificateImgMapper.selectCount(queryCertificateImg);

            if (hktsRealNameAuthentications.get(i).getIdCardPath() == null
                    || hktsRealNameAuthentications.get(i).getDriverLicenseHomePath() == null
                    || hktsRealNameAuthentications.get(i).getDriverLicenseSubPath() == null
                    || hktsRealNameAuthentications.get(i).getBackIdCardPath() == null) {
                continue;
            }
            if (integer == 0) {
                //身份证正面照
                CertificateImg idCardPath = new CertificateImg();
                idCardPath.setId(String.valueOf(flowIdInstance.nextId()));
                idCardPath.setUserId(hktsRealNameAuthentications.get(i).getUserId());
                idCardPath.setDelFlag(0);
                idCardPath.setCheckStatus(1);
                idCardPath.setCertificateType("idCardPath");
                idCardPath.setCardPath(hktsRealNameAuthentications.get(i).getIdCardPath());
                idCardPath.setCreateBy("admin");
                idCardPath.setCreateTime(LocalDateTime.now());
                //身份证反面照
                CertificateImg backIdCardPath = new CertificateImg();
                backIdCardPath.setId(String.valueOf(flowIdInstance.nextId()));
                backIdCardPath.setUserId(hktsRealNameAuthentications.get(i).getUserId());
                backIdCardPath.setDelFlag(0);
                backIdCardPath.setCheckStatus(1);
                backIdCardPath.setCertificateType("backIdCardPath");
                backIdCardPath.setCardPath(hktsRealNameAuthentications.get(i).getBackIdCardPath());
                backIdCardPath.setCreateBy("admin");
                backIdCardPath.setCreateTime(LocalDateTime.now());

                CertificateImg riverLicenseHomePath = new CertificateImg();
                riverLicenseHomePath.setId(String.valueOf(flowIdInstance.nextId()));
                riverLicenseHomePath.setUserId(hktsRealNameAuthentications.get(i).getUserId());
                riverLicenseHomePath.setDelFlag(0);
                riverLicenseHomePath.setCheckStatus(1);
                riverLicenseHomePath.setCertificateType("driverLicenseHomePath");
                riverLicenseHomePath.setCardPath(hktsRealNameAuthentications.get(i).getDriverLicenseHomePath());
                riverLicenseHomePath.setCreateBy("admin");
                riverLicenseHomePath.setCreateTime(LocalDateTime.now());
                //riverLicenseHomePath.setCarId(hktsRealNameAuthentications.get(i).getId());

                //用户驾驶证副业
                CertificateImg driverLicenseSubPath = new CertificateImg();
                driverLicenseSubPath.setId(String.valueOf(flowIdInstance.nextId()));
                driverLicenseSubPath.setUserId(hktsRealNameAuthentications.get(i).getUserId());
                driverLicenseSubPath.setDelFlag(0);
                driverLicenseSubPath.setCheckStatus(1);
                driverLicenseSubPath.setCertificateType("driverLicenseSubPath");
                driverLicenseSubPath.setCardPath(hktsRealNameAuthentications.get(i).getDriverLicenseSubPath());
                driverLicenseSubPath.setCreateBy("admin");
                driverLicenseSubPath.setCreateTime(LocalDateTime.now());
                //driverLicenseSubPath.setCarId(hktsRealNameAuthentications.get(i).getId());
                //用户驾驶证副业
                CertificateImg peopleCarsPath = new CertificateImg();
                peopleCarsPath.setId(String.valueOf(flowIdInstance.nextId()));
                peopleCarsPath.setUserId(hktsRealNameAuthentications.get(i).getUserId());
                peopleCarsPath.setDelFlag(0);
                peopleCarsPath.setCheckStatus(1);
                peopleCarsPath.setCertificateType("peopleCarsPath");
                peopleCarsPath.setCardPath(hktsRealNameAuthentications.get(i).getPeopleCarsPath());
                peopleCarsPath.setCreateBy("admin");
                peopleCarsPath.setCreateTime(LocalDateTime.now());
                peopleCarsPath.setCarId(hktsRealNameAuthentications.get(i).getId());

                CertificateImg copilotPath = new CertificateImg();
                copilotPath.setId(String.valueOf(flowIdInstance.nextId()));
                copilotPath.setUserId(hktsRealNameAuthentications.get(i).getUserId());
                copilotPath.setDelFlag(0);
                copilotPath.setCheckStatus(1);
                copilotPath.setCertificateType("copilotPath");
                copilotPath.setCardPath(hktsRealNameAuthentications.get(i).getCopilotPath());
                copilotPath.setCreateBy("admin");
                copilotPath.setCreateTime(LocalDateTime.now());
                copilotPath.setCarId(hktsRealNameAuthentications.get(i).getId());

                CertificateImg drivingLicensePath = new CertificateImg();
                drivingLicensePath.setId(String.valueOf(flowIdInstance.nextId()));
                drivingLicensePath.setUserId(hktsRealNameAuthentications.get(i).getUserId());
                drivingLicensePath.setDelFlag(0);
                drivingLicensePath.setCheckStatus(1);
                drivingLicensePath.setCertificateType("drivingLicensePath");
                drivingLicensePath.setCardPath(hktsRealNameAuthentications.get(i).getDrivingLicensePath());
                drivingLicensePath.setCreateBy("admin");
                drivingLicensePath.setCreateTime(LocalDateTime.now());
                drivingLicensePath.setCarId(hktsRealNameAuthentications.get(i).getId());
                list.add(drivingLicensePath);
                list.add(copilotPath);
                list.add(peopleCarsPath);
                list.add(idCardPath);
                list.add(backIdCardPath);
                list.add(riverLicenseHomePath);
                list.add(driverLicenseSubPath);
            }
            //处理车辆信息
            //校验该司机是否有该车
            QueryWrapper<CarInfo> queryCarInfo = Wrappers.query();
            queryCarInfo.lambda().eq(CarInfo::getUserId,hktsRealNameAuthentications.get(i).getUserId());
            queryCarInfo.lambda().eq(CarInfo::getCarCode,hktsRealNameAuthentications.get(i).getCarCode());
            Integer integer1 = carInfoMapper.selectCount(queryCarInfo);
            if(integer1==0){
                CarInfo info=new CarInfo();
                info.setId(hktsRealNameAuthentications.get(i).getId());
                HktsDriver byId = iHktsDriverService.getById(hktsRealNameAuthentications.get(i).getUserId());
                if(byId!=null&&byId.getApproval().equals("2")){
                    info.setApprovalStatus(1);
                }else if(byId!=null&&byId.getApproval().equals("3")){
                    info.setApprovalStatus(3);
                }else{
                    info.setApprovalStatus(2);
                }
                info.setApprovalTime(hktsRealNameAuthentications.get(i).getApprovalTime());
                info.setApprovalUser(hktsRealNameAuthentications.get(i).getApprovalUser());
                info.setCarCode(hktsRealNameAuthentications.get(i).getCarCode());
                info.setApprovalType(1);
                info.setUserId(hktsRealNameAuthentications.get(i).getUserId());
                info.setDelFlag(0);
                info.setEhicleType(hktsRealNameAuthentications.get(i).getEhicleType());
                info.setVehicleLength(hktsRealNameAuthentications.get(i).getVehicleLength());
                info.setCreateTime(hktsRealNameAuthentications.get(i).getCreateTime());
                info.setIsAuto(1);
                info.setUpdateBy(hktsRealNameAuthentications.get(i).getUpdateBy());
                info.setUpdateTime(hktsRealNameAuthentications.get(i).getUpdateTime());
                info.setCreateTime(hktsRealNameAuthentications.get(i).getCreateTime());
                info.setCreateBy(hktsRealNameAuthentications.get(i).getCreateBy());
               // listCarInfo.add(info);
                carInfoMapper.insert(info);
            }
        }
        this.saveOrUpdateBatch(list);
        //iCarInfoService.saveOrUpdateBatch(listCarInfo);
    }
}
